
namespace Behemoth.Web.UI.WebControls {

    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

	public class MultiSelector : ListControl {

		protected Table t;
		protected TableRow row1;
		protected TableRow row2;
		protected TableRow row3;
		protected TableRow row4;
		protected TableCell leftCell;
		protected TableCell centerCell1;
		protected TableCell centerCell2;
		protected TableCell centerCell3;
		protected TableCell centerCell4;
		protected TableCell rightCell;

		protected ListBox availableChoices;
		protected ListBox selectedChoices;

		protected override void CreateChildControls() {
			base.CreateChildControls();

			// construct objects
			t = new Table();
			row1 = new TableRow();
			row2 = new TableRow();
			row3 = new TableRow();
			row4 = new TableRow();
			leftCell = new TableCell();
			centerCell1 = new TableCell();
			centerCell2 = new TableCell();
			centerCell3 = new TableCell();
			centerCell4 = new TableCell();
			rightCell = new TableCell();
			// set spans
			leftCell.RowSpan = 4;
			rightCell.RowSpan = 4;
			// add cells to rows
			row1.Cells.Add( leftCell );
			row1.Cells.Add( centerCell1 );
			row1.Cells.Add( rightCell );
			row2.Cells.Add( centerCell2 );
			row3.Cells.Add( centerCell3 );
			row4.Cells.Add( centerCell4 );
			// add rows to table
			t.Rows.Add( row1 );
			t.Rows.Add( row2 );
			t.Rows.Add( row3 );
			t.Rows.Add( row4 );

			// list boxes
			availableChoices = new ListBox();
			selectedChoices = new ListBox();

		}

		#region Properties passed to availableChoices

		public override string AccessKey {
			get { return (availableChoices != null) ? availableChoices.AccessKey : string.Empty; }
			set { availableChoices.AccessKey = value; }
		}

		public override bool AutoPostBack {
			get { return false; }
			set { throw new NotSupportedException("You may not explicitly set this property. It is set by the control logic."); }
		}

		public override bool AppendDataBoundItems {
			get {
				return (availableChoices != null) ?
					availableChoices.AppendDataBoundItems : false;
			}
			set {
				EnsureChildControls();
				availableChoices.AppendDataBoundItems = value;
			}
		}

		/// <summary>
		/// Sets the background color of the table container
		/// </summary>
		public override Color BackColor {
			get {
				return (this.t != null) ? 
					t.BackColor : Color.White;
			}
			set {
				EnsureChildControls();
				t.BackColor = value;
			}
		}

		/// <summary>
		/// Sets the background color of the internal listboxes
		/// </summary>
		public Color ListBoxBackColor {
			get {
				return (availableChoices != null) ?
					availableChoices.BackColor : Color.White;
			}
			set {
				EnsureChildControls();
				availableChoices.BackColor = value;
				selectedChoices.BackColor = value;
			}
		}

		public override Color BorderColor {
			get {
				return (t != null) ?
					t.BackColor : Color.Black;
			}
			set {
				EnsureChildControls();
				t.BorderColor = value;
			}
		}

		public Color ListBoxBorderColor {
			get {
				return (availableChoices != null) ?
					availableChoices.BackColor : Color.Black;
			}
			set {
				EnsureChildControls();
				availableChoices.BorderColor = value;
				selectedChoices.BorderColor = value;
			}
		}

		public override Unit BorderWidth {
			get {
				return (t != null) ?
					t.BorderWidth : Unit.Empty;
			}
			set {
				EnsureChildControls();
				t.BorderWidth = value;
			}
		}

		public Unit ListBoxBorderWidth {
			get {
				return (availableChoices != null) ?
					availableChoices.BorderWidth : Unit.Empty;
			}
			set {
				EnsureChildControls();
				availableChoices.BorderWidth = value;
				selectedChoices.BorderWidth = value;
			}
		}

		public override BorderStyle BorderStyle {
			get {
				return (t != null) ?
					t.BorderStyle : BorderStyle.None;
			}
			set {
				EnsureChildControls();
				t.BorderStyle = value;
			}
		}

		public BorderStyle ListBoxBorderStyle {
			get {
				return (availableChoices != null) ?
					availableChoices.BorderStyle : BorderStyle.None;
			}
			set {
				EnsureChildControls();
				availableChoices.BorderStyle = value;
				selectedChoices.BorderStyle = value;
			}
		}

		public override void ClearSelection() {
			// TODO
			//base.ClearSelection();
		}

		public override string CssClass {
			get {
				return base.CssClass;
			}
			set {
				base.CssClass = value;
			}
		}

		#endregion

	}

}
